package com.cdz.charger.service.impl;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

import com.cdz.charger.vo.ChargingSessionVo;
import com.cdz.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cdz.charger.mapper.ChargingSessionMapper;
import com.cdz.charger.domain.ChargingSession;
import com.cdz.charger.service.IChargingSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 充电会话Service业务层处理
 *
 * @author baefengk2
 * @date 2025-03-10
 */
@Service
public class ChargingSessionServiceImpl extends ServiceImpl<ChargingSessionMapper, ChargingSession> implements IChargingSessionService {
    @Autowired
    private ChargingSessionMapper chargingSessionMapper;

    /**
     * 查询充电会话
     *
     * @param id 充电会话主键
     * @return 充电会话
     */
    @Override
    public ChargingSession selectChargingSessionById(Long id) {
        return getById(id);
    }

    /**
     * 查询充电会话列表
     *
     * @param chargingSession 充电会话
     * @return 充电会话
     */
    @Override
    public List<ChargingSessionVo> selectChargingSessionList(ChargingSession chargingSession) {
        return chargingSessionMapper.selectChargingSessionList(chargingSession);
    }

    /**
     * 新增充电会话
     *
     * @param chargingSession 充电会话
     * @return 结果
     */
    @Override
    public int insertChargingSession(ChargingSession chargingSession) {
        LocalDateTime startTime = chargingSession.getStartTime();
        LocalDateTime endTime = chargingSession.getEndTime();

        // 计算消耗的能量
        // 计算两个时间点之间的时间差
        Duration duration = Duration.between(startTime, endTime);
        //将持续时间转换为分钟
        long minutes = duration.toMinutes();
        // 每十分钟消耗1kWh电量, 因此总消耗
        double energyConsumed = minutes / 10.0;

        chargingSession.setEnergyConsumed(energyConsumed);

        return save(chargingSession) ? 1 : 0;
    }

    /**
     * 修改充电会话
     *
     * @param chargingSession 充电会话
     * @return 结果
     */
    @Override
    public int updateChargingSession(ChargingSession chargingSession) {

        LocalDateTime startTime = chargingSession.getStartTime();
        LocalDateTime endTime = chargingSession.getEndTime();

        // 计算消耗的能量
        // 计算两个时间点之间的时间差
        Duration duration = Duration.between(startTime, endTime);
        //将持续时间转换为分钟
        long minutes = duration.toMinutes();
        // 每十分钟消耗1kWh电量, 因此总消耗
        double energyConsumed = minutes / 10.0;

        chargingSession.setEnergyConsumed(energyConsumed);

        return updateById(chargingSession) ? 1 : 0;
    }

    /**
     * 批量删除充电会话
     *
     * @param ids 需要删除的充电会话主键
     * @return 结果
     */
    @Override
    public int deleteChargingSessionByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除充电会话信息
     *
     * @param id 充电会话主键
     * @return 结果
     */
    @Override
    public int deleteChargingSessionById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
